package com.cxd.ancun.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.cxd.ancun.constants.LoanConstants.RepayStatus;
import com.cxd.ancun.entity.base.InvestRepay;
import com.cxd.ancun.entity.base.RepayRoadmap;
import com.cxd.base.util.ArithUtil;
import com.cxd.base.util.BeanTools;

/**
 * <一句话功能简述> <功能详细描述>
 * @author wtj
 * @date 2016年4月11日 下午5:20:58
 * @company CXD
 * @version [Copyright (c) 2016 V001]
 */
@Service("investService")
public class InvestService {
	private static final Logger logger = Logger.getLogger(LoanService.class);
	@Resource
	public BeanTools db;
	
	
	public List<InvestRepay> getInvestRepays(String investId){
		return db.queryListBean("INVEST_REPAY", "invest_id", investId, InvestRepay.class);
	}
	public RepayRoadmap getRepayRoadmap(String investId) {
		RepayRoadmap repayRoadmap = null;
		List<InvestRepay> investRepays = getInvestRepays(investId); 
		if (investRepays!=null&&investRepays.size()>0) {
			// 未还本金
			Double unPaidCorpus = 0D;
			// 未还利息
			Double unPaidInterest = 0D;
			// 未还手续费
			Double unPaidFee = 0D;
			// 未还罚息
			Double unPaidDefaultInterest = 0D;
			// 已还本金
			Double paidCorpus = 0D;
			// 已还利息
			Double paidInterest = 0D;
			// 已还手续费
			Double paidFee = 0D;
			// 已还罚息
			Double paidDefaultInterest = 0D;
			// 下个还款日
			Date nextRepayDate = null;
			// 下个还款本金
			Double nextRepayCorpus = null;
			// 下个还款利息
			Double nextRepayInterest = null;
			// 下个还款手续费
			Double nextRepayFee = null;
			// 下个还款罚息
			Double nextRepayDefaultInterest = null;
			// 已还期数
			int paidPeriod = 0;
			for (int i = 0; i < investRepays.size(); i++) {
				InvestRepay ir = investRepays.get(i);
				if (ir.getStatus().equals(RepayStatus.BAD_DEBT)
						|| ir.getStatus().equals(RepayStatus.OVERDUE)
						|| ir.getStatus().equals(RepayStatus.REPAYING)) {
					unPaidCorpus = ArithUtil.add(unPaidCorpus, Double.parseDouble(ir.getCorpus()));
					unPaidInterest = ArithUtil.add(unPaidInterest,Double.parseDouble(ir.getInterest()));
					unPaidFee = ArithUtil.add(unPaidFee, Double.parseDouble(ir.getFee()));
					unPaidDefaultInterest = ArithUtil.add(unPaidDefaultInterest, Double.parseDouble(ir.getDefaultInterest()));
					if (i == 0
							|| investRepays.get(i - 1).getStatus()
									.equals(RepayStatus.COMPLETE)) {
						// 下一期待还款
						nextRepayDate = ir.getRepayDay();
						nextRepayCorpus = Double.parseDouble(ir.getCorpus());
						nextRepayInterest = Double.parseDouble(ir.getInterest());
						nextRepayFee = Double.parseDouble(ir.getFee());
						nextRepayDefaultInterest = Double.parseDouble(ir.getDefaultInterest());
					}
				} else if (ir.getStatus().equals(RepayStatus.COMPLETE)) {
					paidCorpus = ArithUtil.add(paidCorpus, Double.parseDouble(ir.getCorpus()));
					paidInterest = ArithUtil.add(paidInterest, Double.parseDouble(ir.getInterest()));
					paidFee = ArithUtil.add(paidFee, Double.parseDouble(ir.getFee()));
					paidDefaultInterest = ArithUtil.add(paidDefaultInterest,Double.parseDouble(ir.getDefaultInterest()));
					paidPeriod++;
				}
			}

			repayRoadmap = new RepayRoadmap(unPaidCorpus, unPaidInterest,
					unPaidFee, unPaidDefaultInterest, paidCorpus, paidInterest,
					paidFee, paidDefaultInterest, nextRepayDate,
					nextRepayCorpus, nextRepayInterest, nextRepayFee,
					nextRepayDefaultInterest, paidPeriod,investRepays
							.size() - paidPeriod, false);
		}
		return repayRoadmap;
	}
}
